package cn.tedu.seckill.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 通过声明大量的对象，使得连接rabbitmq底层的connection可以创建很多组件内容，和调用底层代码queueDeclear exchangeDeclear效果一样
 */
@Configuration
public class QueueCompConfig {
    /**
     * 声明队列对象
     */
    @Bean
    public Queue queue01() {
        // springboot在底层通过连接调用queueDeclear name false false false null
        return new Queue("seckill01", false, false, false, null);
    }

    @Bean
    public Queue queue02() {
        return new Queue("seckill02", false, false, false, null);
    }

    /**
     * 配置声明交换机对象
     */
    @Bean
    public DirectExchange exD1() {
        // 默认不自动删除，支持持久化
        return new DirectExchange("seckillD01");
    }

    @Bean
    public DirectExchange exD2() {
        return new DirectExchange("seckillD02");
    }

    /**
     * 绑定关系，通过返回代码对象实现 Binding
     */
    @Bean
    public Binding bind01() {
        // seckill01使用seckill的路由绑定到了seckillD01
        return BindingBuilder.bind(queue01()).to(exD1()).with("seckill");
    }

    @Bean
    public Binding bind02() {
        return BindingBuilder.bind(queue01()).to(exD2()).with("seckill2");
    }
}
